﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using PostSharp.Aspects;

namespace EasyDLL.AOP.Attributes
{
    /// <summary>
    /// 将标记了该属性的方法加入UI线程中异步执行，常常用于在
    /// 多线程中更新界面状态
    /// </summary>
    [Serializable]
    public class UIThreadAttribute : MethodInterceptionAspect
    {
        public override void OnInvoke(MethodInterceptionArgs args)
        {
            Control main = args.Instance as Control;
            if (main != null && main.InvokeRequired)
            {
                main.BeginInvoke(new ThreadStart(args.Proceed));
            }
            else
            {
                args.Proceed();
            }
        }
    }
}
